# 使用官方的 PHP 7.4 FPM 镜像作为基础镜像
FROM php:7.4-fpm

# 设置工作目录
WORKDIR /var/www/html

# 替换为阿里云镜像源 + 安装系统依赖
# 注意：先做源替换，再 update，避免重复下载
RUN set -xe \
    && sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list \
    && sed -i 's/security.debian.org/mirrors.aliyun.com\/debian-security/g' /etc/apt/sources.list \
    && apt-get update \
    && apt-get install -y --no-install-recommends \
        libpng-dev \
        libjpeg-dev \
        libfreetype6-dev \
        libzip-dev \
        libonig-dev \
        libxml2-dev \
        curl \
        git \
        unzip \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-install pdo_mysql mysqli mbstring bcmath zip xml opcache \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# 安装 Composer（使用国内镜像）
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# 配置 Composer 使用中国镜像
RUN composer config -g repos.packagist composer https://mirrors.aliyun.com/composer/

# =============== 优化缓存的关键：先复制 composer.json ===============
COPY composer.json composer.lock ./
RUN composer install --no-dev --optimize-autoloader --no-scripts \
    && composer clear-cache

# =============== 再复制其余代码（避免 composer install 因代码变更而重新执行） ===============
COPY . .

# 创建 runtime 目录并设置权限（避免 777）
RUN mkdir -p runtime \
    && chown -R www-data:www-data runtime \
    && chmod -R 775 runtime

# 暴露端口
EXPOSE 9000

# 启动命令
CMD ["php-fpm"]